package com.etms.utils;

import com.alibaba.fastjson.JSON;
import com.etms.pojo.*;
import com.etms.vo.RepertoryVo;

import java.io.*;
import java.util.List;

import static java.lang.System.out;

public class DaochuUtils {
    private static String caigoufileloc="D:" + File.separator + "导出信息"+ File.separator+"采购" + File.separator;//采购单导出地址
    private static String xiaoshoufileloc="D:" + File.separator + "导出信息"+File.separator + "销售" + File.separator;//销售单导出地址
    private static String zhangdanfileloc="D:" + File.separator + "导出信息"+File.separator + "账单" + File.separator;//账单导出地址
    public static void main(String[] args) throws IOException {
        //1:使用File类创建一个要操作的文件路径
        File file = new File("D:" + File.separator + "demo" + File.separator + "test.xlsx");
        if(!file.getParentFile().exists()){ //如果文件的目录不存在
            file.getParentFile().mkdirs(); //创建目录
        }

        //2: 实例化OutputString 对象
        OutputStream output = new FileOutputStream(file);
        //3: 准备好实现内容的输出
        AccountMsg accountMsg=new AccountMsg();
        accountMsg.setId(1).setMsg("生产入库账户付费40000.0元").setCreateTime("2022-04-19 16:31:36");

    }

    public static String daochucaigou(int id, RepertoryVo repertoryVo){
        File file = new File(caigoufileloc+"采购单信息——订单号-"+id+".txt");
        if(!file.getParentFile().exists()){ //如果文件的目录不存在
            file.getParentFile().mkdirs(); //创建目录
        }
        OutputStream output=null;
        try {
            //2: 实例化OutputString 对象
            output = new FileOutputStream(file);
            //3: 准备好实现内容的输出
            StringBuffer sb=new StringBuffer();
            sb.append("=====采购单信息=====\n");
            sb.append("=====订单号: "+id+"=========\n");
            Goods_category good = repertoryVo.getGood();
            sb.append("=====货品信息=====\n");
            sb.append("=====货品性质: "+good.getName()+"=====\n");
            sb.append("=====当前该货品库存量: "+good.getSum()+"=====\n");
            sb.append("=====货品所在仓库: "+good.getWid()+"=====\n");
            sb.append("=====货品单价: "+good.getPerPrice()+"元=====\n");
            sb.append("=====本次货品采购量: "+repertoryVo.getSum()+"=====\n");
            sb.append("=====本次货品采购总值: "+repertoryVo.getTotalPrice()+"元=====\n");
            Supplier supplier = repertoryVo.getSupplier();
            sb.append("=====供货商信息=====\n");
            sb.append("=====供货商: "+ supplier.getName()+"=====\n");
            sb.append("=====供货商邮箱: "+ supplier.getEmail()+"=====\n");
            sb.append("=====供货商联系电话: "+ supplier.getPhone()+"=====\n");
            sb.append("=====供货商地址: "+ supplier.getAddr()+"=====\n");
            sb.append("=====采购部门"+repertoryVo.getDept().getName()+"=====\n");
            sb.append("=====管理员是否批准: "+repertoryVo.isPass()+"=====\n");
            sb.append("=====订单达成时间: "+repertoryVo.getUpdateTime()+"=====\n");
            String txt=sb.toString();
            //将字符串变为字节数组
            byte data[] = txt.getBytes();
            try {
                output.write(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //4: 资源操作的最后必须关闭
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return caigoufileloc+"采购单信息——订单号-"+id+".txt";
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String daochuxiaoshou(int id, RepertoryVo repertoryVo){
        File file = new File(xiaoshoufileloc+"销售单信息——订单号-"+id+".txt");
        if(!file.getParentFile().exists()){ //如果文件的目录不存在
            file.getParentFile().mkdirs(); //创建目录
        }
        OutputStream output=null;
        try {
            //2: 实例化OutputString 对象
            output = new FileOutputStream(file);
            //3: 准备好实现内容的输出
            StringBuffer sb=new StringBuffer();
            sb.append("=====采购单信息=====\n");
            sb.append("=====订单号: "+id+"=========\n");
            Goods_category good = repertoryVo.getGood();
            sb.append("=====货品信息=====\n");
            sb.append("=====货品性质: "+good.getName()+"=====\n");
            sb.append("=====当前该货品库存量: "+good.getSum()+"=====\n");
            sb.append("=====货品所在仓库: "+good.getWid()+"=====\n");
            sb.append("=====货品单价: "+good.getPerPrice()+"元=====\n");
            sb.append("=====本次货品采购量: "+repertoryVo.getSum()+"=====\n");
            sb.append("=====本次货品采购总值: "+repertoryVo.getTotalPrice()+"元=====\n");
            Customer customer = repertoryVo.getCustomer();
            sb.append("=====客户信息=====\n");
            sb.append("=====客户: "+ customer.getName()+"=====\n");
            sb.append("=====客户邮箱: "+ customer.getEmail()+"=====\n");
            sb.append("=====客户联系电话: "+ customer.getPhone()+"=====\n");
            sb.append("=====客户地址: "+ customer.getAddr()+"=====\n");
            sb.append("=====销售部门"+repertoryVo.getDept().getName()+"=====\n");
            sb.append("=====管理员是否批准: "+repertoryVo.isPass()+"=====\n");
            sb.append("=====订单达成时间: "+repertoryVo.getUpdateTime()+"=====\n");
            String txt=sb.toString();
            //将字符串变为字节数组
            byte data[] = txt.getBytes();
            try {
                output.write(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //4: 资源操作的最后必须关闭
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return xiaoshoufileloc+"销售单信息——订单号-"+id+".txt";

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String daochuzhangdan(List<AccountMsg> accountMsgList){//txt
        File file = new File(zhangdanfileloc+"账单信息.txt");
        if(!file.getParentFile().exists()){ //如果文件的目录不存在
            file.getParentFile().mkdirs(); //创建目录
        }
        OutputStream output=null;
        try {
            //2: 实例化OutputString 对象
            output = new FileOutputStream(file);
            //3: 准备好实现内容的输出
            StringBuffer sb=new StringBuffer();
            sb.append("=====账单信息=====\n");
            sb.append("账单记录号   日志信息                                时间\n");
            for (AccountMsg accountMsg : accountMsgList) {
                sb.append(accountMsg.getId()+"                "+accountMsg.getMsg()+"   "+accountMsg.getCreateTime()+"\n");
            }
            String txt=sb.toString();
            //将字符串变为字节数组
            byte data[] = txt.getBytes();
            try {
                output.write(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //4: 资源操作的最后必须关闭
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return zhangdanfileloc+"账单信息.txt";

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String daochuzhangdanForExcel(List<AccountMsg> accountMsgList){//写成excel
        String filepath="";

        try {
            WriteExcel writeExcel=new WriteExcel(zhangdanfileloc+"账单信息.xlsx");
            String []titleStr={"账单号","日志信息","时间"};
            writeExcel.write(titleStr,0);
            int i=1;
            for (AccountMsg accountMsg : accountMsgList) {
                String []excelStr=new String[3];
                excelStr[0]=accountMsg.getId().toString();
                excelStr[1]=accountMsg.getMsg();
                excelStr[2]=accountMsg.getCreateTime();
                writeExcel.write(excelStr,i);
                i++;
            }
            filepath=zhangdanfileloc+"账单信息.xlsx";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filepath;
    }
}
